var e = require;
var t = module;
var o = exports;
var ii, nn = this && this.__extends || (ii = function (t, e) {
    return (ii = Object.setPrototypeOf || {
        __proto__: []
    }
        instanceof Array &&
        function (t, e) {
            t.__proto__ = e
        } ||
        function (t, e) {
            for (var o in e) Object.prototype.hasOwnProperty.call(e, o) && (t[o] = e[o])
        })(t, e)
},
    function (t, e) {
        function o() {
            this.constructor = t
        }
        ii(t, e),
            t.prototype = null === e ? Object.create(e) : (o.prototype = e.prototype, new o)
    }
),
    rr = this && this.__awaiter ||
        function (t, e, o, i) {
            return new (o || (o = Promise))(function (n, a) {
                function r(t) {
                    try {
                        l(i.next(t))
                    } catch (e) {
                        a(e)
                    }
                }
                function s(t) {
                    try {
                        l(i.
                            throw(t))
                    } catch (e) {
                        a(e)
                    }
                }
                function l(t) {
                    var e;
                    t.done ? n(t.value) : (e = t.value, e instanceof o ? e : new o(function (t) {
                        t(e)
                    })).then(r, s)
                }
                l((i = i.apply(t, e || [])).next())
            })
        };
var oo = (this && this.__decorate) ||
    function (e, t, a, n) {
        var i,
            o = arguments.length,
            s = o < 3 ? t : null === n ? (n = Object.getOwnPropertyDescriptor(t, a)) : n;
        if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) s = Reflect.decorate(e, t, a, n);
        else
            for (var r = e.length - 1; r >= 0; r--)
                (i = e[r]) && (s = (o < 3 ? i(s) : o > 3 ? i(t, a, s) : i(t, a)) || s);
        return o > 3 && s && Object.defineProperty(t, a, s), s;
    };
Object.defineProperty(o, "__esModule", { value: !0 });
var a = e("BaseModuleUI"),
    n = e("NotifyEventType"),
    i = e("GlobalParams"),
    r = e("AudioManager"),
    s = e("CommonHelper"),
    l = e("PureHelper"),
    _ = e("Game"),
    c = e("RoleSoldierInfo"),
    d = e("node_team"),
    h = e("role_node"),
    u = cc._decorator,
    p = u.ccclass,
    m = u.property,
    f = (function (e) {
        function t() {
            var t = (null !== e && e.apply(this, arguments)) || this;
            return (
                (t.monster_ske = null),
                (t.monster_type = _.MonsterType.red),
                (t.monster_hp_mask = null),
                (t.monster_hp_label = null),
                (t.monster_common_skill = null),
                (t.monster_skill_one = null),
                (t.monster_skill_two = null),
                (t.skill_one_time = 0),
                (t.skill_two_time = 0),
                (t.ack_range = 0),
                (t.monster_info = null),
                (t.max_hp_num = 0),
                (t.now_hp_num = 0),
                (t.is_die = !1),
                (t.role_solider = null),
                (t.team_info = _.TeamInfo.rest_team),
                (t.ai_attack = !1),
                (t.role_move = !1),
                (t.moveDir = new cc.Vec2(0, 0)),
                (t.monster_id = 0),
                (t.d_monster_info = [
                    {},
                    { name: "烈焰犬", head: "11" },
                    { name: "雷电龙狐", head: "12" },
                    {},
                    { name: "机械邪瑞", head: "14" },
                    { name: "冰山巨兽", head: "15" }
                ]),
                t
            );
        }
        return (
            nn(t, e),
            (t.prototype.showMonsterIndex = function (e, t) {
                (this.monster_id = e),
                    (this.monster_info = c.default.D_Moster_info[t]),
                    (this.max_hp_num = this.monster_info.hp),
                    (this.now_hp_num = this.max_hp_num),
                    (this.monster_hp_mask.width = 125),
                    (this.monster_hp_label.string = "" + this.now_hp_num);
            }),
            (t.prototype.initView = function () {
                for (var e = this, t = 0, o = this.node.getComponentsInChildren(cc.BoxCollider); t < o.length; t++) {
                    var a = o[t];
                    1 == a.tag &&
                        ((this.monster_common_skill = a),
                            (a.enabled = !1),
                            (this.ack_range = 1.5 * Math.abs(a.offset.x))),
                        2 == a.tag && ((this.monster_skill_one = a), (a.enabled = !1)),
                        3 == a.tag && ((this.monster_skill_two = a), (a.enabled = !1));
                }
                (this.role_solider = this.node.parent),
                    (this.team_info = this.node.getComponent(d.default).getTeamInfo()),
                    this.monster_ske.setCompleteListener(function (t, o) {
                        e.monsterSkinCompleteListener(t, o);
                    }),
                    2 != i.GlobalParams.MultiModeIndex ||
                    i.GlobalParams.GameStart ||
                    this.scheduleOnce(function () {
                        e.playAnimation("run", !0);
                        var t = e.node.getPosition();
                        (t.x += e.node.scaleX > 0 ? -200 : 200),
                            cc
                                .tween(e.node)
                                .to(1, { position: cc.v2(t.x, t.y) })
                                .call(function () {
                                    e.playAnimation("battle_idle", !0);
                                })
                                .start();
                    }, 1.2),
                    this.scheduleOnce(function () {
                        e.updateTime();
                    }, s.default.getRandom(0, 3.5));
            }),
            (t.prototype.updateTime = function () {
                var e = this;
                i.GlobalParams.GameStart &&
                    (this.is_die || (this.getAttackTarget(), this.role_move && !this.ai_attack && this.roleMove())),
                    this.scheduleOnce(function () {
                        e.updateTime();
                    }, i.GlobalParams.GameUpdateTime);
            }),
            (t.prototype.getAttackTarget = function () {
                for (var e = 9999, t = null, o = 0, a = this.role_solider.children; o < a.length; o++) {
                    var n = a[o],
                        i = n.getComponent(d.default).getTeamInfo();
                    if (cc.isValid(n, !0) && i != this.team_info && i != _.TeamInfo.rest_team) {
                        var r = Math.abs(l.default.Distance(this.node.getPosition(), n.getPosition()));
                        r < e && ((e = r), (t = n));
                    }
                }
                if (null != t) {
                    if ((this.ai_attack || (this.node.scaleX = this.node.x > t.x ? 1 : -1), e > 1400))
                        this.roleMoveInfo(cc.v2(-1 * this.node.scaleX, 0));
                    else if (e > this.ack_range) {
                        var s = cc.v2(t.x, t.y).sub(this.node.getPosition()).normalize();
                        this.roleMoveInfo(s);
                    } else
                        Math.abs(t.y - this.node.y) > 50
                            ? this.roleMoveInfo(cc.v2(0, t.y > this.node.y ? 1 : -1))
                            : this.ai_attack || (this.roleStopMove(), (this.ai_attack = !0), this.AISpecialSkill());
                } else this.roleStopMove();
            }),
            (t.prototype.roleMoveInfo = function (e) {
                i.GlobalParams.GameStart &&
                    ((this.moveDir = e),
                        this.role_move || this.ai_attack || ((this.role_move = !0), this.playAnimation("run", !0)));
            }),
            (t.prototype.roleStopMove = function () {
                this.role_move &&
                    !this.ai_attack &&
                    (this.node.stopAllActions(), this.playAnimation("battle_idle", !0)),
                    (this.role_move = !1);
            }),
            (t.prototype.roleMove = function () {
                0 != this.moveDir.x && (this.node.scaleX = Math.abs(this.node.scaleX) * (this.moveDir.x < 0 ? 1 : -1));
                var e = this.node.getPosition().add(this.moveDir.mul(30));
                cc.tween(this.node)
                    .to(i.GlobalParams.GameUpdateTime, { position: cc.v2(e.x, e.y) })
                    .start();
            }),
            (t.prototype.attackMonster = function (e) {
                this.emitEvent(n.default.SEND_MONSTER_BE_HIT_NUM_SHOW_HP_INFO, {
                    hit_num: e,
                    monster_id: this.monster_id,
                    max_hp: this.now_hp_num,
                    name: this.d_monster_info[this.monster_type].name,
                    head: this.d_monster_info[this.monster_type].head
                }),
                    (this.now_hp_num -= e),
                    this.emitEvent(n.default.SEND_SHOW_BE_HIT_INFO, {
                        attack: e,
                        my_team: 0,
                        end_pos: cc.v2(this.node.getPosition().x, this.node.getPosition().y + 80)
                    }),
                    (this.now_hp_num = this.now_hp_num < 0 ? 0 : this.now_hp_num),
                    (this.monster_hp_mask.width = (125 * this.now_hp_num) / this.max_hp_num),
                    (this.monster_hp_label.string = "" + this.now_hp_num),
                    this.now_hp_num <= 0 &&
                    !this.is_die &&
                    ((this.is_die = !0),
                        r.default.getInstance().playFx("monster_die"),
                        this.emitEvent(n.default.SEND_MONSTER_DIE_NUMBER_INFO, this.node.getPosition()),
                        this.playAnimation("death", !1));
            }),
            (t.prototype.AISpecialSkill = function () {
                this.skill_one_time <= 0 && this.monster_skill_one
                    ? this.monsterSpecialSkillOne()
                    : this.skill_two_time <= 0 && this.monster_skill_two
                        ? this.monsterSpecialSkillTwo()
                        : this.monsterAttackInfo("attack");
            }),
            (t.prototype.monsterSpecialSkillOne = function () {
                this.monsterAttackInfo("attack_skill1");
            }),
            (t.prototype.monsterSpecialSkillTwo = function () {
                this.monsterAttackInfo("attack_skill2");
            }),
            (t.prototype.monsterAttackInfo = function (e) {
                var t = this,
                    o = this.monster_ske.setAnimation(0, e, !0);
                this.monster_ske.setTrackEventListener(o, function () {
                    t.monstrtCommonSkill(!1, e), t.monstrtCommonSkill(!0, e);
                });
            }),
            (t.prototype.monsterSkinCompleteListener = function (e) {
                var t = e.animation ? e.animation.name : "";
                -1 != ["attack", "attack_skill1", "attack_skill2"].indexOf(t) &&
                    (this.monstrtCommonSkill(!1, t),
                        (this.monster_ske.timeScale = 1),
                        this.monster_ske.clearTracks(),
                        this.playAnimation("battle_idle", !0),
                        (this.ai_attack = !1),
                        this.skillCoolingTime(t)),
                    "death" == t && this.node.destroy();
            }),
            (t.prototype.skillCoolingTime = function (e) {
                var t = this;
                "attack_skill1" == e
                    ? ((this.skill_one_time = 5),
                        this.scheduleOnce(function () {
                            t.skill_one_time = 0;
                        }, this.skill_one_time))
                    : "attack_skill2" == e &&
                    ((this.skill_two_time = 12),
                        this.scheduleOnce(function () {
                            t.skill_two_time = 0;
                        }, this.skill_two_time));
            }),
            (t.prototype.monstrtCommonSkill = function (e, t) {
                (this.monster_common_skill.enabled = e && "attack" == t),
                    this.monster_skill_one && (this.monster_skill_one.enabled = e && "attack_skill1" == t),
                    this.monster_skill_two && (this.monster_skill_two.enabled = e && "attack_skill2" == t);
            }),
            (t.prototype.playAnimation = function (e, t) {
                "death" != this.monster_ske.animation && this.monster_ske.setAnimation(0, e, t);
            }),
            (t.prototype.onCollisionEnter = function (e, t) {
                var o = e.node,
                    a = o.group;
                if (cc.isValid(o, !0) && (1 == t.tag || 2 == t.tag || 3 == t.tag) && 0 == e.tag && "role" == a) {
                    var n = s.default.getRandomInt(this.monster_info.attack[0], this.monster_info.attack[1]);
                    o.getComponent(h.default).roleBeHit(n, 0);
                }
            }),
            oo([m(sp.Skeleton)], t.prototype, "monster_ske", void 0),
            oo(
                [m({ type: cc.Enum(_.MonsterType), displayName: "野怪类型" })],
                t.prototype,
                "monster_type",
                void 0
            ),
            oo([m(cc.Node)], t.prototype, "monster_hp_mask", void 0),
            oo([m(cc.Label)], t.prototype, "monster_hp_label", void 0),
            oo([p], t)
        );
    })(a.default);
o.default = f;
